<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-03-14 19:49:16 +0100 (Fri, 14 Mar 2008) $
 * $Rev: 1017 $
 * $Author: gavinspearhead $
 * $Id: functions.php 1017 2008-03-14 18:49:16Z gavinspearhead $
 */

if (!defined('ORIGINAL_PAGE')) die('This file cannot be accessed directly.');


$pathbut = realpath(dirname(__FILE__));


require_once "$pathbut/defines.php";
require_once "$pathbut/autoincludes.php";


class button {
    public $img;
    public $url;
    public $name;
    function __construct($n, $i, $u)
    {
        $this->img = $i;
        $this->url = $u;
        $this->name = $n;
    }
}


function insert_default_buttons(DatabaseConnection $db)
{
    $default_buttons = array (
        new button('All4Divx',                  'buttons/btn_all4divx.png',         'http://www.all4divx.com/subtitles/$q/any/1'),
        new button('Answers',                   'buttons/btn_answers.png',          'http://www.answers.com/$q'),
        new button('Binsearch',                 'buttons/btn_binsearch.png',        'http://binsearch.net/?q=$q&max=25&adv_age=600&server='),
        new button('Ebay',                      'buttons/btn_ebay.png',             'http://search.ebay.com/$q'),
        new button('FreeDB',                    'buttons/btn_freedb.png',           'http://www.freedb.org/freedb_search.php?words=$q&allfields=NO&fields=artist&fields=title&allcats=YES&grouping=none'),
        new button('Google',                    'buttons/btn_google.png',           'http://www.google.com/search?q=$q'),
        new button('IMDB',                      'buttons/btn_imdb.png',             'http://www.google.com/search?btnI&amp;q=site:imdb.com%20%22$q%22'),
        new button('MetalArchives',             'buttons/btn_metalarchives.png',    'http://www.metal-archives.com/search.php?string=$q&searchfrom=main&type=band'),
        new button('MusicBrainz',               'buttons/btn_musicbrainz.png',      'http://musicbrainz.org/search/textsearch.html?query=$q&type=artist&an=1&as=1'),
        new button('OpenSubs',                  'buttons/btn_opensubtitles.png',    'http://www.google.com/search?btnI&q=site:opensubtitles.org%20%22$q%22'),
        new button('RLS',                       'buttons/btn_releaselog.png',       'http://www.rlslog.net/?s=$q'),
        new button('Rotten Tomatoes',           'buttons/btn_rotten.png',           'http://www.rottentomatoes.com/search/full_search.php?search=$q'),
        new button('TV Rage',                   'buttons/btn_tvrage.png',           'http://www.tvrage.com/search.php?search=$q'),
        new button('Wikipedia (English)',       'buttons/btn_wikipedia_en.png',     'http://en.wikipedia.org/wiki/Special:Search?search=$q'),
        new button('Wikipedia (Nederlands)',    'buttons/btn_wikipedia_nl.png',     'http://nl.wikipedia.org/wiki/Special:Search?search=$q'),
        new button('Wikipedia (Deutsch)',       'buttons/btn_wikipedia_de.png',     'http://de.wikipedia.org/wiki/Special:Search?search=$q'),
        new button('Wikipedia (Francais)',      'buttons/btn_wikipedia_fr.png',     'http://fr.wikipedia.org/wiki/Special:Search?search=$q'),
        new button('Yahoo',                     'buttons/btn_yahoo.png',            'http://search.yahoo.com/search?p=$q&ei=UTF-8&r=moz2'),
        new button('Youtube',                   'buttons/btn_youtube.png',          'http://www.google.com/search?btnI&q=site:youtube.com%20$q')
    );

    foreach($default_buttons as $b)
        add_button($db, $b->name, $b->img, $b->url);
}


function add_button(DatabaseConnection $db, $name, $image_url, $search_url)
{
    $res = $db->insert_query('searchbuttons', array ('name', 'image_url', 'search_url'), array ($name, $image_url, $search_url));
}

function update_button(DatabaseConnection $db, $name, $image_url, $search_url, $id)
{
    assert(is_numeric($id));
    $db->escape($image_url, TRUE);
    $db->escape($search_url, TRUE);
    $db->escape($name, TRUE);
    $db->escape($id, TRUE);
    $query = "UPDATE searchbuttons SET \"name\"=$name, \"image_url\"=$image_url, \"search_url\"=$search_url WHERE \"id\"=$id";
    $res = $db->execute_query($query);
}


function delete_button(DatabaseConnection$db, $id)
{
    assert(is_numeric($id));
    $db->escape($id, TRUE);
    $query = "DELETE FROM searchbuttons WHERE \"id\"=$id";
    $db->execute_query($query);
}


function apply_button($q_url, $msg)
{
    // $q  is the string we replace with the $msg
    $url = str_replace('$q', htmlspecialchars($msg), $q_url);
    return $url;
}

?>
